﻿using System;
using PTop.Code.Machine;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Text;
using System.Web.Mvc;

namespace PTop.Web.Areas.SystemSecurity.Controllers
{
    public class ServerMonitoringController : ControllerBase
    {

        [DllImport("kernel32")]
        public static extern void GetSystemDirectory(StringBuilder SysDir, int count);
        [DllImport("kernel32")]
        public static extern void GetSystemInfo(ref  CPU_INFO cpuinfo);
        [DllImport("kernel32")]
        public static extern void GlobalMemoryStatus(ref  MEMORY_INFO meminfo);
        [DllImport("kernel32")]
        public static extern void GetSystemTime(ref  SYSTEMTIME_INFO stinfo);

        public ActionResult GetServerInfo()
        {
            var sp = new ComputorInfo();

            //内存
            var memInfo = new MEMORY_INFO();
            GlobalMemoryStatus(ref  memInfo);
            sp.MemoryPercentage = memInfo.dwMemoryLoad;

            //CPU
            try
            {
                var cpu = new PerformanceCounter("Processor", "% Processor Time", "_Total");
                sp.CpuPercentage = cpu.NextValue() * 100;
            }
            catch (Exception ex)
            {

            }

            return Success("Success", sp);
        }
    }

}
